package com.example.chat.dao;

import com.dragonsoftbravo.jpadsl.JPADslBaseDao;
import com.dragonsoftbravo.jpadsl.PredicateBuilder;
import com.example.chat.model.QUser;
import com.example.chat.model.User;
import com.example.chat.model.ro.UserListRO;
import com.example.chat.model.vo.UserListVO;
import com.example.chat.type.DataStatusEnum;
import com.querydsl.core.types.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;

/**
 * @author pq
 * @date 2022/8/18 17:23
 * @description
 * @modified
 **/
@Repository
public class UserDao extends JPADslBaseDao<User, String> {

    private final QUser user = QUser.user;

    @Autowired
    public UserDao(EntityManager entityManager) {
        setEntityManager(entityManager);
    }

    @Override
    public QUser basePath() {
        return QUser.user;
    }

    public static PredicateBuilder buildQ() {
        PredicateBuilder pb = new PredicateBuilder();
        return pb;
    }

    public Page<UserListVO> findUserListByPage(UserListRO ro){
        PredicateBuilder pb=buildQ().eq(user.dataStatus, DataStatusEnum.effective.getCode());
        pb.like(user.nickname,ro.getNickname());
        return selectPageable(queryFactory().select(Projections.bean(
                UserListVO.class,
                user.account.as("userNumber"),
                user.nickname.as("nickname"),
                user.avatar.as("userAvatar")
                )).from(user).where(pb.get()).orderBy(user.createDate.desc().nullsLast()),ro.pageable());
    };
}
